package cn.itcast.manage;

import cn.itcast.bean.Student;
import cn.itcast.dao.WriteDemo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/listStudentServlet")
public class listStudentServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        读取文档
//        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("d:\\student.txt"));
//        ServletOutputStream os = resp.getOutputStream();
//        byte[] bytes = new byte[1024];
//        int len;
//        while ((len = bis.read(bytes))!= -1){
//            os.write(bytes,0,len);
//        }
//        bis.close();
        WriteDemo wd = new WriteDemo();
        ArrayList<Student> list = wd.getList();
        HttpSession session = req.getSession();
        session.setAttribute("students",list);
        resp.sendRedirect(req.getContextPath() + "/listStudent.jsp");
//        resp.setContentType("text/html;charset=UTF-8");
//        for (Student student : list) {
//            resp.getWriter().println(student);
//        }
//        跳转页面
//        resp.setContentType("text/html;charset=UTF-8");
//        resp.getWriter().write("");
}

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
